package com.sysu.minsysu.xyxw;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.sysu.minsysu.R;
import com.sysu.minsysu.utils.Rotate3dAnimation;

public class XiaoYuanXiaoWu extends Activity{

	SimpleAdapter mAdapter;
	WebView mWebView;
	GridView mGridView;
	LinearLayout mIconLayout;
	//RelativeLayout mAnimLayout;
	LinearLayout mContentLayout;
	RelativeLayout mContainer;
	ImageView[] mIconViews;
	View[] mItemViews;
	//ImageView[] mAnimViews;
	
	String rawString = "file:///android_asset/";
	String[] htmNames = new String[]{
			"xiaoqubanche.htm",
			"shizaizhongda.htm",
			"zhongdaxiaoli.htm",
			"xiaowutongxun.htm",
			"searchbook.html",
			"zhongdagaikuang.htm",
			"shuziziliao.htm",
	};
	
	boolean isWebViewShowing=false;
	AnimationSet[] animations=null;
	
	@Override
	public void onCreate( Bundle savedInstanceState ){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gridmainlayout);
		mContainer = (RelativeLayout)findViewById(R.id.layout);
		mGridView = (GridView)findViewById(R.id.gridview);
		mWebView = (WebView)findViewById(R.id.webView);
		mIconLayout = (LinearLayout)findViewById(R.id.icon_title);
		mContentLayout = (LinearLayout)findViewById(R.id.content);
		//mAnimLayout = (RelativeLayout)findViewById(R.id.animationlayout);
		
		mAdapter = new SimpleAdapter(this, getData(), R.layout.view_appmain, new String[]{ "image", "text" }, new int[]{R.id.imageView,R.id.textView});
		mGridView.setAdapter(mAdapter);
		mGridView.setNumColumns(3);		
		
		mGridView.setOnItemClickListener(new MyOnItemClickListener());
		setIconTitle();		
		
	}
	
	private void applyRotation(int position, float start, float end) {
        // Find the center of the container
        final float centerX = mContainer.getWidth() / 2.0f;
        final float centerY = mContainer.getHeight() / 2.0f;

        // Create a new 3D rotation with the supplied parameter
        // The animation listener is used to trigger the next animation
        final Rotate3dAnimation rotation =
                new Rotate3dAnimation(start, end, centerX, centerY, 310.0f, true);
        rotation.setDuration(500);
        //rotation.setFillAfter(true);
        rotation.setInterpolator(new AccelerateInterpolator());
        rotation.setAnimationListener(new ChangeView(position));

        mContainer.startAnimation(rotation);
    }
	
	private final class ChangeView implements Animation.AnimationListener {
        private final int mPosition;

        private ChangeView(int position) {
            mPosition = position;
        }

        public void onAnimationStart(Animation animation) {
        	
        }

        public void onAnimationEnd(Animation animation) {
        	if( mPosition>=0 ){
        		mGridView.setVisibility(View.GONE);
                mContentLayout.setVisibility(View.VISIBLE);
                isWebViewShowing=true;
        		mContentLayout.post(new SwapViews());
        	}
        	else{
        		mContentLayout.setVisibility(View.GONE);
                mGridView.setVisibility(View.VISIBLE);
                isWebViewShowing=false;
        		mGridView.post(new SwapViews());
        	}
        }

        public void onAnimationRepeat(Animation animation) {
        }
    }
	
	private final class SwapViews implements Runnable {
        
        public void run() {
            final float centerX = mContentLayout.getWidth() / 2.0f;
            final float centerY = mContentLayout.getHeight() / 2.0f;
            Rotate3dAnimation rotation;
            
            if (!isWebViewShowing) {        
                rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 310.0f, false);
            } else {                
                rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 310.0f, false);
            }

            rotation.setDuration(500);
            rotation.setFillAfter(true);
            rotation.setInterpolator(new DecelerateInterpolator());

            mContainer.startAnimation(rotation);
        }
    }

	int mIconCount;
	private void setIconTitle(){
		mIconCount = ids.length;
		mIconViews = new ImageView[mIconCount];
		//mAnimViews = new ImageView[mIconCount];
		switch (mIconCount) {
		case 8: mIconViews[7] = (ImageView)mIconLayout.findViewById(R.id.icon7);
			//mAnimViews[5] = (ImageView)mAnimLayout.findViewById(R.id.icon5_anim);
		case 7: mIconViews[6] = (ImageView)mIconLayout.findViewById(R.id.icon6);
			//mAnimViews[5] = (ImageView)mAnimLayout.findViewById(R.id.icon5_anim);
		case 6: mIconViews[5] = (ImageView)mIconLayout.findViewById(R.id.icon5);
				//mAnimViews[5] = (ImageView)mAnimLayout.findViewById(R.id.icon5_anim);
		case 5: mIconViews[4] = (ImageView)mIconLayout.findViewById(R.id.icon4);
				//mAnimViews[4] = (ImageView)mAnimLayout.findViewById(R.id.icon4_anim);
		case 4: mIconViews[3] = (ImageView)mIconLayout.findViewById(R.id.icon3);
				//mAnimViews[3] = (ImageView)mAnimLayout.findViewById(R.id.icon3_anim);
		case 3: mIconViews[2] = (ImageView)mIconLayout.findViewById(R.id.icon2);
				//mAnimViews[2] = (ImageView)mAnimLayout.findViewById(R.id.icon2_anim);
		case 2: mIconViews[1] = (ImageView)mIconLayout.findViewById(R.id.icon1);
				//mAnimViews[1] = (ImageView)mAnimLayout.findViewById(R.id.icon1_anim);
		case 1: mIconViews[0] = (ImageView)mIconLayout.findViewById(R.id.icon0);
				//mAnimViews[0] = (ImageView)mAnimLayout.findViewById(R.id.icon0_anim);
			break;
		}
		
		for( int i=0; i<mIconCount; i++ ){
			mIconViews[i].setVisibility(View.VISIBLE);
			mIconViews[i].setOnClickListener( new MyTitleIconClickListener(i) );
			mIconViews[i].setImageResource(ids[i]);
			//mAnimViews[i].setImageResource(ids[i]);
		}
	}
	
	private class MyTitleIconClickListener implements OnClickListener{

		int num;
		public MyTitleIconClickListener( int n ){
			num=n;
		}
		@Override
		public void onClick(View v) {
			//applyTitileItemAnimation(num);
			mGridView.performItemClick(null, num, 0);
		}
		
	}
	
	private void applyTitileItemAnimation( int num ){
		AnimationSet set = new AnimationSet(true);
		//AlphaAnimation animation1 = new AlphaAnimation(0.7f, 0);
		TranslateAnimation animation1 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, -1, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0);
		//animation1.
		animation1.setFillAfter(false);
		animation1.setDuration(250);
		TranslateAnimation animation2 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0);
		animation2.setDuration(250);
		animation2.setStartOffset(250);
		set.addAnimation(animation1);
		set.addAnimation(animation2);
		set.setInterpolator(this, android.R.anim.decelerate_interpolator);
		mWebView.startAnimation(set);
	}

	private class MyOnItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
			
//			if( position==5 ){
//				Intent intent = new Intent(XiaoYuanXiaoWu.this, YiDongXueXi.class);
////				Intent intent = new Intent( Intent.ACTION_VIEW);
////				String type = "video/*";
////				Uri name = Uri.parse("file:///sdcard/test.wmv");
////				intent.setDataAndType(name, type);
//				startActivity(intent);
//				return;
//			}
		
		    String url = rawString + htmNames[position];
		    mWebView.loadUrl(url);
			
			if( !isWebViewShowing)
				applyRotation( position, 0, 90);
		}
	}
	
	@Override
	public boolean onKeyDown( int keyCode, KeyEvent event){
		if( keyCode== KeyEvent.KEYCODE_BACK && isWebViewShowing ){
			mWebView.loadUrl(null);
			applyRotation(-1, 0, 90);
			return true;
		}else if( keyCode==KeyEvent.KEYCODE_BACK){
			return false;
		}
		else return super.onKeyDown(keyCode, event);
	}
	
	int[] ids = new int[]{
			R.drawable.xiaoyuanwenhua_banchechaxun,
			R.drawable.xiaoyuanwenhua_shizaizhongda,
			R.drawable.xiaoyuanwenhua_zhongdaxiaoli,
			R.drawable.xiaoyuanwenhua_xiaowutongxun,
			R.drawable.xiaoyuanwenhua_tushuchaxun,
//			R.drawable.yidongxuexi,
			R.drawable.xiaoyuanwenhua_guanyuzhongda,
			R.drawable.xiaoyuanwenhua_shizililiang,
	};
	String[] names = new String[]{
			"校区班车",
			"食在中大",
			"中大校历",
			"校务通讯",
			"图书查询",
//			"移动学习",
			"中大概况",
			"数字资料",
	};
	
	private List< Map<String, Object> > getData(){
		List< Map<String, Object>> list = new ArrayList<Map<String,Object>>();
			
		for( int i=0; i<ids.length; i++ ){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("image", ids[i]);
		map.put("text", names[i]);
		list.add(map);
		}
			
		return list;
	}
}
